<?php
namespace app\model\frontend;

use think\Model;

use think\facade\Db;
class CouponModel extends model{



    // 添加优惠券
    public static function add( $customer_id, $data ){

        $sql = 'insert into sh_coupon(
                      `customer_id`,
                      `code`,
                      `amount`,
                      
                      `created_at`,
                      `updated_at`) values ';

        $now = date('Y-m-d H:i:s');

        $number = intval( $data['number']);
        $number = !empty($number) ? $number : 1;

        $sql_values = '';
        for ($i = 0; $i < $number; $i ++ ){
            $sql_values .= !empty($sql_values)?',':'';
            $code = self::getRandomString(4) . $i . $customer_id ;

            $sql_values .= '('
                . $customer_id
                .',"' . $code .'"'
                .',' . $data['amount']
                .',"' . $now .'"'
                .',"' . $now .'"'
                . ')';
        }
        $sql .= $sql_values;
        // return sm_pdo_run(  $sql );

        return Db::execute( $sql );
    }

    // 获取用户优惠券列表
    public static function getList( $customer_id, $filter=[] ){

        $now = date("Y-m-d H:i:s");
        $condition = [
            'customer_id = '.$customer_id,
//            'used=0',
            '(start_at <="' . $now . '" or start_at is null)' ,
            '(end_at >="' . $now . '" or end_at is null)' ,

        ];
        if( isset($filter['used'] ) ){
            $condition[] = 'used=' . $filter['used'];
        }
        $orderby = 'used asc,created_at desc';
        return sm_pdo_getall('coupon',
            implode(' and ', $condition),
            [],
            '',
            $orderby
        );

    }


    public static function single( $id ){

        global $_W;
        $customer_id = $_W['customer']['id'];
        $now = date('Y-m-d H:i:s');
        $condition = [
            'id='.$id,
            'customer_id = '.$customer_id,
            'used=0',
            '(start_at <="' . $now . '" or start_at is null)' ,
            '(end_at >="' . $now . '" or end_at is null)' ,

        ];
        return sm_pdo_get('coupon',
            implode(' and ', $condition)
        );
    }

    public static function consume( $coupon_id ){

        $now = date('Y-m-d H:i:s');
        $sql = 'update sh_coupon set used=1,updated_at="'. $now .'" where id=' . $coupon_id;
        // return pdo_query( $sql );
        return Db::execute( $sql );
    }

    public static function getRandomString($len, $chars=null)
    {
        if (is_null($chars)) {
            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        }
        mt_srand(10000000*(double)microtime());
        for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++) {
            $str .= $chars[mt_rand(0, $lc)];
        }
        return $str;
    }
}